package com.personal.system.filter;

//extends UsernamePasswordAuthenticationFilter
public class TokenLoginFilter  {

//    private RedisTemplate redisTemplate;
//
//    private LoginLogService loginLogService;
//
//    @Override
//    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//        System.out.println("进来自定义的过滤器了");
//        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/admin/system/index/login", HttpMethod.POST.name()));
//        super.doFilter(req, res, chain);
//    }
//
//    //构造
//    public TokenLoginFilter(AuthenticationManager authenticationManager,
//                            RedisTemplate redisTemplate,
//                            LoginLogService loginLogService) {
//        this.setAuthenticationManager(authenticationManager);
//        this.setPostOnly(false);
//        //指定登录接口及提交方式，可以指定任意路径
////        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/admin/system/index/login","POST"));
//        this.redisTemplate = redisTemplate;
//        this.loginLogService = loginLogService;
//    }
//
//    //获取用户名和密码，认证
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request,
//                                                HttpServletResponse response) {
//        try {
//            LoginVo loginVo = new ObjectMapper().readValue(request.getInputStream(), LoginVo.class);
//
//            Authentication authenticationToken = new UsernamePasswordAuthenticationToken(loginVo.getUsername(), loginVo.getPassword());
//            return this.getAuthenticationManager().authenticate(authenticationToken);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    //认证成功调用
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
//                                            FilterChain chain, Authentication auth)  {
//        //获取认证对象
//        CustomUser customUser = (CustomUser)auth.getPrincipal();
//
//        //保存权限数据
//        redisTemplate.opsForValue().set(customUser.getUsername(), JSON.toJSONString(customUser.getAuthorities()));
//
//        //生成token
//        String token = JwtHelper.createToken(customUser.getSysUser().getId(), customUser.getSysUser().getUsername());
//
//        //记录登录日志
//        loginLogService.recordLoginLog(customUser.getUsername(),1,
//                IpUtil.getIpAddress(request),"登录成功");
//        //返回
//        Map<String,Object> map = new HashMap<>();
//        map.put("token",token);
//        ResponseUtil.out(response,Result.ok(map));
//    }
//
//    //认证失败
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
//        if(e.getCause() instanceof RuntimeException) {
//            ResponseUtil.out(response, Result.build(null, 204, e.getMessage()));
//        } else {
//            ResponseUtil.out(response, Result.build(null, ResultCodeEnum.LOGIN_MOBLE_ERROR));
//        }
//    }
}
